package servlets;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import actionForm.CreatePhoneValidationForm;
import domain.IDAOPhoneNumber;

public class CreatePhoneAction extends Action {
	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest request,
			final HttpServletResponse response) {
		if (request.getMethod().equals("POST")) {
			final CreatePhoneValidationForm form = (CreatePhoneValidationForm) pForm;
			String contactId = form.getContactId();
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServlet().getServletContext());
			IDAOPhoneNumber daoPhone = (IDAOPhoneNumber) context.getBean("daophonenumber");
			daoPhone.addPhoneToContact( Long.valueOf(contactId), form.getPhoneKind(), form.getPhoneNumber());
			return new ActionRedirect(pMapping.findForward("modifyContact")).addParameter("id", contactId); 
		}
		else if (request.getMethod().equals("GET")){
			request.setAttribute("contactId", request.getParameter("contactId"));	
			return pMapping.findForward("createPhoneNumber");
		}
		return null;
	}
}